package com.ledi.guns.modular.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ledi.guns.common.exception.AppCheckException;
import com.ledi.guns.common.persistence.dao.RoutesMapper;
import com.ledi.guns.common.persistence.dao.RoutesRoleMapper;
import com.ledi.guns.common.persistence.model.vo.RoutesVo;
import com.ledi.guns.core.shiro.ShiroKit;
import com.ledi.guns.modular.system.service.IRoutesService;

/**
 * routesService
 *
 * @author fengshuonan
 * @Date 2018-03-20 11:18:25
 */
@Service
public class RoutesServiceImpl implements IRoutesService {

	@Resource
	private RoutesMapper routesMapper;

	@Resource
	private RoutesRoleMapper routesRoleMapper;

	@Override
	public List<RoutesVo> selectMenus(Long roleId) {

		// 获取菜单列表
		List<Integer> roleList = ShiroKit.getUser().getRoleList();
		if (roleList == null || roleList.size() == 0) {
			ShiroKit.getSubject().logout();
			throw new AppCheckException("system.user.role.code", "system.user.role.message");
		}
		if (roleList.indexOf(roleId.intValue()) < 0) {
			if (roleId == 6) {
				if (roleList.indexOf(7) < 0) {
					throw new AppCheckException("system.user.role.notFind.code", "system.user.role.notFind.message");
				}
			} else {
				throw new AppCheckException("system.user.role.notFind.code", "system.user.role.notFind.message");
			}
		}

		List<RoutesVo> routes = routesMapper.getRoutesByRoleId(roleId);
		if (routes == null || routes.isEmpty()) {
			if (roleId == 6) {
				routes = routesMapper.getRoutesByRoleId(7L);
			}
		}
		List<RoutesVo> titles = RoutesVo.buildTitle(routes);
		return titles;
	}

	@Override
	@Transactional(readOnly = false)
	public void delRoutesById(Long routesId) {

		// 删除路由
		routesMapper.deleteById(routesId);

		// 删除该路由所有的权限
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("routesid", routesId);
		routesRoleMapper.deleteByMap(param);
	}

	/**
	 * 方法描述: [获取用户所在平台角色对应的所有菜单.]<br/>
	 * 初始作者: 钟小平<br/>
	 * 创建日期: 2018年8月21日-下午3:42:15<br/>
	 * 开始版本: 2.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 */
	@Override
	public Map<String, List<String>> selectMenuMap(Integer type) {

		// 获取菜单列表
		List<Integer> roleList = ShiroKit.getUser().getRoleList();
		if (roleList == null || roleList.size() == 0) {
			throw new AppCheckException("system.user.role.code", "system.user.role.message");
		}
		Map<String, List<String>> result = new HashMap<String, List<String>>();
		switch (type) {
			case 0:
				Integer KroleId = 6;
				Integer troleId = 7;
				List<String> teacher = routesRoleMapper.selectMenuByRoleId(troleId);
				List<String> adm = routesRoleMapper.selectMenuByRoleId(KroleId);
				result.put("teacher", teacher);
				result.put("admin", adm);
				break;
			case 1:
				Integer aroleId = 8;
				Integer sroleId = 13;
				List<String> adm1 = routesRoleMapper.selectMenuByRoleId(aroleId);
				List<String> emp = routesRoleMapper.selectMenuByRoleId(sroleId);
				result.put("admin", adm1);
				result.put("worker", emp);
				break;
			case 2:
				Integer caroleId = 9;
				Integer croleId = 11;
				List<String> ca = routesRoleMapper.selectMenuByRoleId(caroleId);
				List<String> cr = routesRoleMapper.selectMenuByRoleId(croleId);
				result.put("admin", ca);
				result.put("worker", cr);
				break;

			default:
				break;
		}
		return result;
	}
}
